package net.huashitong.user.service;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.StringUtils;
import jodd.util.StringUtil;
import net.huashitong.Project.service.EarlyWarningNodeService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.entity.NodeNewtemplet;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.service.NodeNewtempletService;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.service.GradePointService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.Department;
import net.huashitong.user.entity.Role;
import net.huashitong.user.filter.BnpUserFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;

import net.huashitong.user.dao.BnpUserDao;
import net.huashitong.user.entity.BnpUser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BnpUserService extends StringPKService<BnpUser> {
    @Autowired
    private BnpUserDao dao;
    @Lazy
    @Autowired
    private NodeNewschedulesService nns;
    @Lazy
    @Autowired
    private NodeNewtempletService newtempletService;
    @Lazy
    @Autowired
    private SupervisionTaskService taskService;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;

    @Override
    protected BaseDao<BnpUser, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(BnpUser obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_user", null));
    }

    /**
     * 用户查询
     *
     * @param filter
     * @return
     */
    public List<BnpUser> getUserInfo(BnpUserFilter filter) {
        if (filter.getPage() == null) {
            filter.setPage(0);
        }
        Paging p = new Paging();
        p.setCurrentPage(filter.getPage() + 1);
        filter.setPaging(p);
        return dao.getUserInfo(filter);
    }

    public List<BnpUser> getUserInfoUsePage(BnpUserFilter filter) {
        return dao.getUserInfo(filter);
    }

    public List<BnpUser> queryUserInfo2(BnpUserFilter filter) {
        return dao.queryUserInfo2(filter);
    }

    public BnpUser getBnpUser(String username, String password) {
        return dao.getBnpUser(username, password);
    }

    public BnpUser getBnpUserByName(String sysId) {
        return dao.getBnpUserByName(sysId);
    }

    /**
     * 求总页数
     *
     * @param filter
     * @return
     */
    public double getBnpUserSum(BnpUserFilter filter) {
//        BnpUserFilter  bnpUserCount = dao.getBnpUserCount(filter);
        double count = (double) dao.getBnpUserCount(filter);
        double totalPage = Math.ceil(count / 10);//进一位
        return totalPage;
    }

    public BnpUser getBnpUserByPhone(String phone) {
        return dao.getBnpUserByPhone(phone);
    }

    public BnpUser getBnpUserByPhoneAndName(String phone, String name) {
        return dao.getBnpUserByPhoneAndName(phone, name);
    }

    public List<BnpUser> queryByFilter(BnpUserFilter filter) {
        return dao.queryByFilter(filter);
    }

    public List<String> queryAllBranchs() {
        List<String> list = dao.queryAllBranchs();
        List<String> resList = new ArrayList<>();
        for (String str : list) {
            if (StringUtils.isNotBlank(str)) {
                resList.add(str);
            }
        }
        return resList;
    }

    public List<BnpUser> queryBranchsByType(BnpUserFilter filter) {
        return dao.queryBranchsByType(filter);
    }

    public List<String> getBranchInAllUser() {
        return dao.getBranchInAllUser();
    }


    /**
     * 修改用户以下表对应用户联动修改
     *
     * @param filter
     * @param oldBnpUser
     * @return
     */
    public boolean updateUserInAll(BnpUserFilter filter, BnpUser oldBnpUser) {

        String phone = oldBnpUser.getPhone();
        String name = oldBnpUser.getName();
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(name)) {
            return false;
        }
        //--节点清单表-----------------------------------------------------------------
        //经办人
        List<NodeNewschedules> operatorNodes = nns.queryByOperator(phone, name);
        for (NodeNewschedules operatorNode : operatorNodes) {
            operatorNode.setOperatorPhone(filter.getPhone());
            operatorNode.setOperatorUser(filter.getName());
        }
        //保存
        nns.batchUpdate(operatorNodes);
        //责任人
        List<NodeNewschedules> resUserNodes = nns.queryByResUser(phone, name);
        for (NodeNewschedules resUserNode : resUserNodes) {
            resUserNode.setResUserPhone(filter.getPhone());
            resUserNode.setResUser(filter.getName());
        }
        //保存
        nns.batchUpdate(resUserNodes);
        //--节点模板表-----------------------------------------------------------------
        //经办人
        List<NodeNewtemplet> templets1 = newtempletService.queryByOperator(phone, name);
        for (NodeNewtemplet templet : templets1) {
            templet.setOperatorPhone(filter.getPhone());
            templet.setOperatorUser(filter.getName());
        }
        //保存
        newtempletService.batchUpdate(templets1);
        //责任人
        List<NodeNewtemplet> templets2 = newtempletService.queryByResUser(phone, name);
        for (NodeNewtemplet templet : templets2) {
            templet.setResUserPhone(filter.getPhone());
            templet.setResUser(filter.getName());
        }
        //保存
        newtempletService.batchUpdate(templets2);
        //--督查表---------------------------------------------------------
        //经办人
        List<SupervisionTask> tasks1 = taskService.queryByOperator(phone, name);
        for (SupervisionTask task : tasks1) {
            task.setOperatorPhone(filter.getPhone());
            task.setOperatorUser(filter.getName());
        }
        //保存
        taskService.batchUpdate(tasks1);
        //责任人
        List<SupervisionTask> tasks2 = taskService.queryByResUser(phone, name);
        for (SupervisionTask task : tasks2) {
            task.setResPhone(filter.getPhone());
            task.setResUser(filter.getName());
        }
        //保存
        taskService.batchUpdate(tasks1);
        //--绩效表---------------------------------------------------------
        //--预警表（发送短信）---------------------------------------------------------
        return true;
    }

    public List<BnpUser> dealWithAddDeptName(List<BnpUser> list) {
        List<Department> departments = departmentService.query();
        List<Role> roles = roleService.query();
        Map<String, Role> roleMap = new HashMap<>();
        for (Role role : roles) {
            if(!roleMap.containsKey(role.getSysId())){
                roleMap.put(role.getSysId(),role);
            }
        }
        Map<String, Department> departmentMap = new HashMap<>();
        for (Department department : departments) {
            if (!departmentMap.containsKey(department.getSysId())) {
                departmentMap.put(department.getSysId(), department);
            }
        }
        for (BnpUser bnpUser : list) {
            if (StringUtil.isNotBlank(bnpUser.getDeptId())) {
                String deptName = departmentMap.get(bnpUser.getDeptId()).getDeptName();
                bnpUser.setDeptName(deptName);
            }
            if (StringUtil.isNotBlank(bnpUser.getRoleId())) {
                String roleName = roleMap.get(bnpUser.getRoleId()).getRoleName();
                bnpUser.setRoleName(roleName);
            }
        }
        return list;
    }
}
